
package com.barnabe;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextArea;

/* @author Arthur F. Zanona */

public class Servidor extends Thread {
    private ServerSocket ss ;
    private JLabel imageLabel ;    
    private boolean ativo = true;   
    private Consumidor consumidor = new Consumidor();
    private final List<BufferedImage> buffer = new ArrayList<BufferedImage>();
    private Semaphore podeConsumir = new Semaphore(0);
    private boolean continua = true;
    public static JTextArea log ;

    public void encerrar() {
        this.continua = false;
        consumidor.interrupt();
        this.interrupt();
        try {
            ss.close();
        } catch (IOException ex) {
        }
    }

    public class Consumidor extends Thread {

        @Override
        public void run() {
            super.run();
            try {
                while ( buffer.size() > 0 || continua ) {
                    podeConsumir.acquire();
                    // pegando imgem do buffer
                    BufferedImage i;
                    synchronized (buffer){
                        i = buffer.remove(0);
                    }
                    IdentificarCamera.executar(i);
                    imageLabel.setIcon(new ImageIcon( i ));
                    imageLabel.repaint();
                    i = null;
                }
            } catch (InterruptedException ex) {
                log.append(ex.getMessage() + "\n");
            }           
            finally {
                //imageLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/barnabe/icons/nerd.jpg")));
            }
        }
        
    }


    public Servidor(JLabel imageLabel ) {
        this.imageLabel = imageLabel;
        this.consumidor.start();
    }

    public boolean isAtivo() {
        return ativo;
    }

    @Override
    public void run() {
        super.run();
        Socket s;
        try {
            ss = new ServerSocket(3030);
            ativo = true;
            while ( continua ) {
                try {                    
                    s = ss.accept();
                    try {
                        BufferedImage i = ImageIO.read( s.getInputStream() );
                        // bufferizando
                        synchronized(buffer){
                            buffer.add(i);
                        }
                        i = null;
                        // liberando consumidor
                        podeConsumir.release();
                    }
                    finally {
                        s.close(); }
                }
                catch (IOException ex) { 
                    log.append(ex.getMessage() + "\n");
                }
            }
        }
        
        catch (IOException ex) {
            ativo = false;
            log.append(ex.getMessage() + "\n");
        }
        finally {
            try {
                ss.close();
                this.continua = false;
            } catch (IOException ex) {log.append(ex.getMessage() + "\n");}
        }

        
    }


    
}
